<?php
/**
 * Created by PhpStorm.
 * User: 曹乐清
 * Date: 2017/3/20
 * Time: 15:55
 */

namespace Home\Controller;


use Think\Auth;
use Think\Controller;

class ProductController extends CommonController
{
    //@todo 产品首页
    public function index(){
        import('ORG.Util.Auth');//加载类库
        $auth=new Auth();
        if(!$auth->check('Home/Product/upload_product_file',$_SESSION['user']['id'])){
            $this->assign('dis','no');
        }else{
            $this->assign('dis','yes');
        }
        $this->display();
    }

    //@todo 产品类别
    public function add_product(){
        $name=$_POST['name'];
        $data=array(
            'product_type'=>$_POST['type'],
            'name'=>$name,
            'ac_time'=>time()
        );
        $id=D('product')->add($data);
        if(!$id){
            $this->ajaxReturn(array('success'=>false,'message'=>'新增失败,请稍后再试'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'新增成功'));
    }

    //@todo 获取分裂列表
    public function get_product_list(){
        $total=D('product')->count();
        $row=D('product')->field('*,FROM_UNIXTIME(ac_time,"%Y-%m-%d %H:%m:%i") as ac_time')->order('id desc')->select();
        echo json_encode(array('total'=>$total,'rows'=>$row));
    }

    //@todo 上传产品图片
    public function upload_product_file(){
        $id=$_GET['id'];
        $import=new \Think\Upload(C('ProductConfig'));//实例化上传类
        $info=$import->upload();//图片上传操作
        if(!$info){
            $this->ajaxReturn(array('success' =>'error'));
        }
        //生成缩略图
        $thumb= new \Think\Image();
        $pic=$info[0]['savepath'].$info[0]['savename'];
        $uploadSrc=C('ProductConfig')['rootPath'].$pic;
        $thumb->open($uploadSrc);
        $thumb->thumb(250, 250)->save(C('ProductConfig')['rootPath'].$info[0]['savepath'].'thumb_'.$info[0]['savename']);
        $data=array();
        $data['pid']=$id;
        $data['pic']=$pic;
        $data['thumb']=$info[0]['savepath'].'thumb_'.$info[0]['savename'];
        $data['ac_time']=time();
        $id=D('product_pic')->add($data);
        if(!$id){
            $this->ajaxReturn(array('success' =>'error'));
        }
        $this->ajaxReturn(array('success' =>'success'));
    }

    //@todo 查看产品图片集
    public function get_pic_data(){
        $id=$_GET['id'];
        $data=D('product_pic')->where(array('pid'=>$id))->select();
        $row=array();
        $row['title']='产品图';
        $row['id']=$id;
        $row['start']=0;
        foreach($data as $key=>$value){
            $i=$key+1;
            $res[]=array(
                'alt'=>'第'.$i.'张',
                'pid'=>$value['id'],
                'src'=>'/'.'Uploads/Cms/productPic/'.$value['pic'],
                'thumb'=>'/'.'Uploads/Cms/productPic/'.$value['thumb']
            );
        }
        $row['data']=$res;
        echo json_encode($row);
    }

    //@todo 查看产品列表页
    public function get_detail_list(){
        $id=I('get.id');
        $name=D('product')->find($id);
        $this->assign('id',$id);
        $this->assign('name',$name['name']);
        $this->display();
    }

    //@todo 获取产品数据
    public function getlist(){
        $id=$_GET['id'];
        $start=$_GET['offset'];
        $limit=$_GET['limit'];
        $count=D('product_pic')->where(array('pid'=>$id))->count();
        $list=D('product_pic')->field('*,FROM_UNIXTIME(ac_time,"%Y-%m-%d %H:%m:%i") as ac_time')
            ->where(array('pid'=>$id))->limit($start,$limit)->order('id','desc')->select();

        foreach($list as $key=>$value){
            $list[$key]['thumb_url']=__ROOT__.'/'.'Uploads/Cms/productPic/'.$value['thumb'];
        }
        echo json_encode(array('rows'=>$list,'total'=>$count));
    }

    //@todo 查看大图
    public function showBigPhoto(){
        $id=I('get.id');
        $list=D('product_pic')->field('*,FROM_UNIXTIME(ac_time,"%Y-%m-%d %H:%m:%i") as ac_time')
            ->where(array('pid'=>$id))->select();
        $row=array();
        foreach($list as $key=>$value){
            $row[$key]['caption']=$value['ac_time'];
            $row[$key]['name']='/Uploads/Cms/productPic/'.$value['pic'];
        }
        file_put_contents('data/list.json',json_encode($row));
        $this->display();
    }

    // @todo 删除该张图片
    public function del_photo(){
        $id=I('get.id');
        $info=D('product_pic')->where(array('id'=>$id))->find();
        $thumb='./Uploads/Cms/productPic/'.$info['thumb'];
        $pic='./Uploads/Cms/productPic/'.$info['pic'];
        if(is_file($thumb) && is_file($pic)){
            unlink($thumb);unlink($pic);
            $del=D('product_pic')->delete($id);
            if(!$del){
                $this->ajaxReturn(array('success'=>false,'message'=>'删除失败，服务端发生错误'));
            }
            $this->ajaxReturn(array('success'=>true,'message'=>'删除成功'));
        }else{
            $this->ajaxReturn(array('success'=>false,'message'=>'删除失败，服务端发生错误1'));
        }
    }

    //@todo 删除大类
    public function del_Product(){
        $id=I('post.id');
        //查询该大类下的产品信息
        $product_goods_list=D('product_pic')->where(array('pid'=>$id))->select();
        //如果查询到则删除
        if($product_goods_list){
            foreach($product_goods_list as $key =>$value){
                $thumb='./Uploads/Cms/productPic/'.$value['thumb'];
                $pic='./Uploads/Cms/productPic/'.$value['pic'];
                if(is_file($thumb)&&is_file($pic)){
                    unlink($thumb);unlink($pic);
                    $del_goods=D('product_pic')->delete($value['id']);
                }
            }
        }
        //否则直接删除
        $del=D('product')->delete($id);
        if(!$del){
            $this->ajaxReturn(array('success'=>false,'message'=>'删除失败,请稍后再试!'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'删除成功'));
    }
}